package com.poi.demo.excel;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.poi.demo.FileKit;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * excel 文件导出
 * <p>
 * 王永吉
 */
public class ExcelExport {

    public static void main(String[] args) throws IOException {
        //        最简单的excel 文件导出
        simpleExport();
    }

    private static void simpleExport() throws IOException {
        String deskTopPath = FileKit.deskTopPath();
        String filePath = deskTopPath + "/" + new Random().nextInt(10000) + ".xls";
        File file = new File(filePath);
        OutputStream outputStream = new FileOutputStream(file);

        //创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Sheet1");

        //设置表头
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("排名");
        row.createCell(1).setCellValue("员工名");
        row.createCell(2).setCellValue("员工号");
        row.createCell(3).setCellValue("开卡数量");

        //设置每一列的宽高，可以展开表头
        sheet.setColumnWidth(0, (row.getCell(0).getStringCellValue().getBytes().length * 256) + 100);
        sheet.setColumnWidth(1, (row.getCell(1).getStringCellValue().getBytes().length * 256) + 100);
        sheet.setColumnWidth(2, (row.getCell(2).getStringCellValue().getBytes().length * 256) + 100);
        sheet.setColumnWidth(3, (row.getCell(3).getStringCellValue().getBytes().length * 256) + 100);

        //        sheet.setDefaultColumnStyle(column, style);

        //        //设置样式为纯文本
        ExcelKit.setStyleStr(workbook, 0, 0);
        ExcelKit.setStyleStr(workbook, 0, 1);
        ExcelKit.setStyleStr(workbook, 0, 2);
        ExcelKit.setStyleStr(workbook, 0, 3);


        //设置数据
        JSONArray json = JSONArray.parseArray("[{\"0\":1.0,\"1\":\"员工1\",\"2\":\"00001\",\"3\":100.0},{\"0\":2.0,\"1\":\"员工2\",\"2\":\"00002\",\"3\":58.0},{\"0\":3.0,\"1\":\"员工3\",\"2\":\"00003\",\"3\":32.0},{\"0\":4.0,\"1\":\"员工4\",\"2\":\"00004\",\"3\":32.0}]");

        for (int i = 0; i < json.size(); i++) {
            JSONObject jsonObject = json.getJSONObject(i);
            HSSFRow row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(jsonObject.getString("0"));
            row1.createCell(1).setCellValue(jsonObject.getString("1"));
            row1.createCell(2).setCellValue(jsonObject.getString("2"));
            row1.createCell(3).setCellValue(jsonObject.getString("3"));
        }
//        sheet.getRow(2).getCell(2).setCellStyle(style);
//        sheet.getRow(3).getCell(2).setCellStyle(style);


        //默认打开sheet索引
        workbook.setActiveSheet(0);

        //输出文件
        workbook.write(outputStream);
        outputStream.close();
    }
}
